package com.cysyz.web.modules.base.permission.domain;


import com.cysyz.mylib.domain.BaseEntity;
import com.cysyz.mylib.utils.tree.TreeAble;
import com.cysyz.web.modules.base.permission.support.PrivilegeType;
import com.cysyz.web.modules.base.permission.support.ShowStatus;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * SysPrivilege entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "FNM_SYS_PRIVILEGE", uniqueConstraints = @UniqueConstraint(columnNames = "CODE"))
public class SysPrivilege extends BaseEntity implements TreeAble<SysPrivilege, Long>{

	// Fields
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "PARENT_ID")
	private SysPrivilege sysPrivilege;
	@Column(name = "NAME", nullable = false, length = 30)
	private String name;
	@Column(name = "CODE", unique = true, nullable = false, length = 150)
	private String code;
	@Column(name = "DESCRIPTION", length = 100)
	private String description;
	@Column(name = "TYPE")
	private PrivilegeType type;
	@Column(name = "ICON_PATH", length = 200)
	private String iconPath;
	@Column(name = "URL", length = 200)
	private String url;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "MENU_PRIVILEGE_ID")
	private SysPrivilege menuPrivilege;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "sysPrivilege")
	private Set<SysSysrolePrivilege> sysSysrolePrivileges = new HashSet<SysSysrolePrivilege>(
			0);

	@Enumerated
	@Column(name = "SHOW_STATUS", nullable = false, length = 1)
	private ShowStatus showStatus = ShowStatus.SHOW;

	// Constructors

	/** default constructor */
	public SysPrivilege() {
	}

	/** minimal constructor */
	public SysPrivilege(String name, String code) {
		this.name = name;
		this.code = code;

	}

	/** full constructor */
	public SysPrivilege(SysPrivilege sysPrivilege, String name, String code,
			String description, String iconPath, String url,
			Set<SysSysrolePrivilege> sysSysrolePrivileges) {
		this.sysPrivilege = sysPrivilege;
		this.name = name;
		this.code = code;
		this.description = description;

		this.iconPath = iconPath;

		this.url = url;
		this.sysSysrolePrivileges = sysSysrolePrivileges;
	}

	@Override
	public String[] businessKeys() {
		return new String[]{"code"};
	}


	public SysPrivilege getSysPrivilege() {
		return this.sysPrivilege;
	}

	public void setSysPrivilege(SysPrivilege sysPrivilege) {
		this.sysPrivilege = sysPrivilege;
	}


	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}


	public String getCode() {
		return this.code;
	}

	public void setCode(String code) {
		this.code = code;
	}


	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public PrivilegeType getType() {
		return type;
	}

	public void setType(PrivilegeType type) {
		this.type = type;
	}

	public String getIconPath() {
		return this.iconPath;
	}

	public void setIconPath(String iconPath) {
		this.iconPath = iconPath;
	}

	public SysPrivilege getMenuPrivilege() {
		return menuPrivilege;
	}

	public void setMenuPrivilege(SysPrivilege menuPrivilege) {
		this.menuPrivilege = menuPrivilege;
	}

	public String getUrl() {
		return this.url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public ShowStatus getShowStatus() {
		return showStatus;
	}

	public void setShowStatus(ShowStatus showStatus) {
		this.showStatus = showStatus;
	}

	public Set<SysSysrolePrivilege> getSysSysrolePrivileges() {
		return this.sysSysrolePrivileges;
	}

	public void setSysSysrolePrivileges(
			Set<SysSysrolePrivilege> sysSysrolePrivileges) {
		this.sysSysrolePrivileges = sysSysrolePrivileges;
	}

	/*vo*/
	@Transient
	private Long parentId;

	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	@Transient
	public boolean isMenuBar() {
		/*if (getType() != P.NORMAL && sysPrivilege!= null && sysPrivilege.getParentId().equals(0)) {
			return true;
		}*/
		return false;
	}

	@Override
	public Long getPid() {
		Long retVal = parentId;
		if (retVal == null && sysPrivilege != null) {
			retVal = sysPrivilege.getId();
		}
		return retVal;
	}

	@Transient
	private Integer showStatusInt;


	public Integer getShowStatusInt() {
		return showStatusInt;
	}

	public void setShowStatusInt(Integer showStatusInt) {
		this.showStatusInt = showStatusInt;
	}

	@Transient
	private List<SysPrivilege> childPrivilegeDto = new ArrayList<SysPrivilege>();

	@Override
	public void addChild(SysPrivilege node) {
		childPrivilegeDto.add(node);
	}

	@Override
	public boolean hasParent() {
		if (parentId != null || this.sysPrivilege != null) {
			return true;
		}
		return false;
	}

	@Transient
	private boolean roleHasThisPrivilege;

	public void setRoleHasThisPrivilege(boolean roleHasThisPrivilege) {
		this.roleHasThisPrivilege = roleHasThisPrivilege;
	}

	public boolean isRoleHasThisPrivilege() {
		return roleHasThisPrivilege;
	}

	public List<SysPrivilege> getChildPrivilegeDto() {
		return childPrivilegeDto;
	}

	public void setChildPrivilegeDto(List<SysPrivilege> childPrivilegeDto) {
		this.childPrivilegeDto = childPrivilegeDto;
	}
}